package com.tanhua.controller;

import com.tanhua.exception.BusinessException;
import com.tanhua.server.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Map;

@RestController
@RequestMapping("/user")
public class LoginController {
        @Autowired
        private UserService userService;
        /*
        * ResponseEntity是spring提供的一个返回值对象，通过这个对象可以指定返回的状态码，也可以手动设置效应码
        *
        * @RequestBody是将Map(前端传入)从json对象转化成类对象
        * */
    @PostMapping("/login")
    public ResponseEntity login(@RequestBody Map map){
        String phone = (String) map.get("phone");
        userService.sendMsg(phone);
       //return ResponseEntity.status(500).body("出错了");
        return  ResponseEntity.ok(null);
    }
    @PostMapping("/loginVerification")
    public ResponseEntity loginVerification(@RequestBody Map map) throws BusinessException {

            String phone =(String)map.get("phone");
            String code =(String)map.get("verificationCode");
            Map retMap= userService.loginVerification(phone,code);
            return ResponseEntity.ok(retMap);

    }

}
